//
//  XMNoticeListViewController.m
//  XunMi
//
//  Created by MacBook on 2024/3/26.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMNoticeListViewController.h"
#import "XMNoticeListTableViewCell.h"
#import "XMNoticeModel.h"

@interface XMNoticeListViewController ()
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray <XMNoticeModel *> *dataArray;
@end

@implementation XMNoticeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self noticeList];
    [self setupRefresh];
}

#pragma mark - refresh
- (void)setupRefresh {
    XMWeakSelf(self);
    self.tableView.mj_header = [XMRefreshNormalHeader headerWithRefreshingBlock:^{
        XMStrongSelf(self);
        [self noticeListRefresh];
    }];
    self.tableView.mj_footer = [XMRefreshBackFooter footerWithRefreshingBlock:^{
        XMStrongSelf(self);
        self.page += 1;
        [self noticeList];
    }];
    self.tableView.mj_footer.hidden = YES;
}

- (void)noticeListRefresh {
    self.page = 1;
    [self.dataArray removeAllObjects];
    [self noticeList];
}

#pragma mark -
- (void)noticeList {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"page_no":@(self.page),
        @"page_size":@(10)
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/config/noticeList" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        [self.tableView hideStatusView];
        if (tip.success) {
            [MBProgressHUD hideHUD];
            NSArray *record = [result safe_objectForKey:@"record"];
            NSArray *array = [XMNoticeModel mj_objectArrayWithKeyValuesArray:record];
            [self.dataArray addObjectsFromArray:array];
            if (self.dataArray.count == 0) {
                [self.tableView showStatusView:(YHViewStatusTypeEmptyNotice)];
            } else {
                [self.dataArray enumerateObjectsUsingBlock:^(XMNoticeModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (!obj.is_see) {
                        self.navRightBtn.selected = YES;
                        *stop = YES;
                        return;
                    }
                }];
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        if (tip.loadMore) {
            [self.tableView.mj_footer endRefreshing];
        } else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - 初始化变量
- (void)initVariables {
    self.page = 1;
    [self tableViewRegisterNibWithNibName:kStringFromClass(XMNoticeListTableViewCell)];
}
#pragma mark - setupSubViews
- (void)setupSubviews {
    [self.navLeft2Btn setTitle:@"公告" forState:UIControlStateNormal];
    self.navRightBtn.adjustsImageWhenHighlighted = NO;
    [self.navRightBtn setImage:kGetImage(@"notice_clear") forState:UIControlStateNormal];
    [self.navRightBtn setImage:kGetImage(@"notice_clear_s") forState:UIControlStateSelected];    [self.navRightBtn addTarget:self action:@selector(clear) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - Actions
- (void)clear {
    [XMAlertHelper alertTitle:@"是否全部已读?" message:nil cancelTitle:@"取消" sureTitle:@"确定" cancelAction:^{
    } sureAction:^{
        [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
        NSMutableDictionary *dic = @{}.mutableCopy;
        [YHNetworkManager.sharedInstance postWithUrl:@"api/user/notice/clear" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
            if (tip.success) {
                [MBProgressHUD hideHUD];
                self.navRightBtn.selected = NO;
                [self noticeListRefresh];
            }
        }];
    }];
}

#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 86;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMNoticeListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMNoticeListTableViewCell class])];
    cell.model = [self.dataArray safe_objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XMNoticeModel *model = [self.dataArray safe_objectAtIndex:indexPath.row];
    if (model.link.length > 0) {
        XMWebViewController *vc = [[XMWebViewController alloc] init];
        vc.titleStr = model.title;
        vc.urlStr = model.link;
        vc.needNav = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (NSMutableArray<XMNoticeModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
